<template>
  <div class="login">
    <div class="box">
      <h2>后台管理系统</h2>
      <el-form ref="loginRef" :model="loginForm" :rules="loginRules">
        <el-form-item label="姓名" prop="username">
          <el-input v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item class="right">
          <el-button @click="loginBtn" type="text">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        //绑定的
        username: "",
        password: "",
      },
      loginRules: {
        //正则验证
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 2, max: 15, message: "长度在 2到 15 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {},
  methods: {
    loginBtn() {
      this.$refs.loginRef.validate(async (valid) => {
        if (!valid) return false;
        const res = await this.$API.getLoginApi(this.loginForm);
        console.log("验证登录:", res);
        this.$router.push("./Home");
      });
    },
  },
  components: {},
};
</script>

<style lang='scss'>
.login {
  width: 100%;
  height: 100%;
  background-color: #b4c3d4;
  position: absolute;
}
.box {
  width: 400px;
  height: 300px;
  background-color: #fff;
  padding: 20px;
  position: relative;
  top: 25%;
  left: 25%;
  border-radius: 20px;
}
h2 {
  margin: 0;
  margin-bottom: 10px;
}
.right {
  text-align: right;
  margin-bottom: 20px;
}
</style>
